<?php
class Model_Table_News extends Zend_Db_Table{
	
	protected $_name = 'news';
	protected $_primary = 'id';
	
	public function allNews($options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('news')->order('id DESC');
		if($options != null){			
			if(isset($options['limit'])) $select->limit($options['limit'],0);
		}
		$result = $db->fetchAll($select);
		return $result;
	}
	public function getNew($arrParam = null,$options = null){
		$db = $this->getAdapter();
		$select = $db->select()->from('news')->where('id =' . $arrParam['id']);
		return $db->fetchRow($select);
	}
	public function addNew($arrParam = null,$options = null){
		$now = time();
		$systemInfo = new Zendvn_System_Info();
		$username = $systemInfo->getMemberInfo('username');
		
		$row = $this->fetchNew();
		$row->title = $arrParam['title'];
		$row->lead = $arrParam['lead'];
		$row->content = $arrParam['content'];
		$row->created = time();
		$row->created_by = $username;
		$row->status = $arrParam['status'];
		$row->save();
	}
	public function deleteItem($arrParam = null,$options = null){
		if($options == 'multy'){
			$cid[] = $arrParam['cid'];
			$ids = $cid[0];
			foreach ($ids as $val){
				$this->deleteOneItem($val);
			}			
		}else{
			$this->deleteOneItem($arrParam['id']);
		}
	}
	public function deleteOneItem($id){
		$where = 'id =' . $id;
		$this->delete($where);
	}
	public function editItem($arrParam = null,$options = null){
		$now = time();
		$systemInfo = new Zendvn_System_Info();
		$username = $systemInfo->getMemberInfo('username');
		$where = 'id =' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);
		$row->title = $arrParam['title'];
		$row->lead = $arrParam['lead'];
		$row->content = $arrParam['content'];
		$row->status = $arrParam['status'];
		$row->modified = time();
		$row->modified_by = $username;
		$row->save();
	}
	public function changeStatus($arrParam = null,$options = null){
		$status = $arrParam['status'];
		$where = 'id =' . (int)$arrParam['id'];
		$row = $this->fetchRow($where);
		$row->status = $status;
		$row->save();
	}
}